import { GetterTree, ActionTree, MutationTree, Commit } from 'vuex'

import { AdminState } from '@/store/modules/cms/types'

import { setStorage, getStorage, removeStorage} from '@/utils'

import * as api from '@/http/cms'

export const getters:GetterTree<AdminState, any> = {
  /**
   * 是否已登录
   *
   * @param state
   */
  is_logged_in: (state: AdminState): boolean => {
    return !!(state.access_token && state.user_info.id && (state.user_info.username || state.user_info.mobile))
  },
  
  /**
   * 令牌
   *
   * @param state
   */
  token: (state: AdminState): string => state.access_token
}

export const mutations: MutationTree<AdminState> = {
  /**
   * 设定 token 和用户信息
   *
   * @param state
   * @param data
   */
  setTokenAndUserInfo(state: AdminState, data: any) {
    state.access_token = data.access_token
    state.user_info = data.user_info
    setStorage(state.storage.access_token, JSON.stringify(state.access_token))
    setStorage(state.storage.user_info, JSON.stringify(state.user_info))
  },
  
  /**
   * 获取 token 和用户信息
   * @param state
   */
  getTokenAndUserInfo(state: AdminState) {
    if (getStorage(state.storage.access_token) && getStorage(state.storage.user_info)) {
      state.access_token = JSON.parse(getStorage(state.storage.access_token))
      state.user_info = JSON.parse(getStorage(state.storage.user_info))
    }
  },
  
  /**
   * 注销
   */
  logout(state: AdminState) {
    removeStorage(state.storage.access_token)
    removeStorage(state.storage.user_info)
    removeStorage(state.storage.sidebar_menu)
    state.access_token = ''
    state.user_info = {}
    state.sidebar_menu = []
  },
  
  /**
   * 设置用户信息
   *
   * @param state
   * @param data
   */
  setUserInfo(state: AdminState, data: any) {
    console.log(data)
    if (data.user_info) {
      state.user_info = data.user_info
      setStorage(state.storage.user_info, JSON.stringify(state.user_info))
    }
    
  },
  
  /**
   * 设定侧栏菜单
   *
   * @param {AdminState} state 状态
   * @param data
   */
  setSidebarMenu(state: AdminState, data: any) {
    state.sidebar_menu = data
    setStorage(state.storage.sidebar_menu, JSON.stringify(state.sidebar_menu))
  },
  
  /**
   * 读取侧栏菜单
   *
   * @param state
   */
  getSidebarMenu(state: AdminState) {
    if (getStorage(state.storage.sidebar_menu)) {
      state.sidebar_menu = JSON.parse(getStorage(state.storage.sidebar_menu))
    }
  }
}

export const actions: ActionTree<AdminState, any> = {
  /**
   * 登录
   *
   * @param commit
   * @param payload
   */
  login({commit}: {commit: Commit}, payload: any): Promise<any> {
    return api.login(payload.param).then(res => {
      if (res.status === 200) {
        commit('setTokenAndUserInfo', res.data)
      }
      
      return res
    })
  },
  
  /**
   * 获取授权
   *
   * @param commit
   */
  getAuth({commit}: {commit: Commit}): void {
    commit('getTokenAndUserInfo')
  },
  
  /**
   * 注销
   *
   * @param commit
   */
  logout({commit}: {commit: Commit}): void {
    commit('logout')
  },
  
  /**
   * 侧栏
   *
   * @param commit
   * @param payload
   */
  sidebarMenu({commit}: {commit: Commit}, payload: any): Promise<any> {
    return api.sidebarMenu(payload.param).then(res => {
      if (res.status === 200) {
        commit('setSidebarMenu', res.data)
      }
    
      return res
    })
  },
  
  /**
   * 获取侧栏菜单
   *
   * @param commit
   */
  getSidebarMenu({commit}: {commit: Commit}): void {
    commit('getSidebarMenu')
  }
}

export const state: AdminState = {
  storage: {
    access_token: 'cms_access_token',
    user_info: 'cms_user_info',
    sidebar_menu: 'cms_sidebar_menu'
  },
  access_token: '',
  is_logged_in: false,
  user_info: {
    id: 0,
    username: '',
    mobile: '',
    email: '',
    avatar: '',
    is_super: false,
    roles: []
  },
  sidebar_menu: []
}
